# To change this template, choose Tools | Templates
# and open the template in the editor.
import sys
from PyQt4 import QtGui,QtCore,Qt

class FilterableList(QtGui.QWidget):
    def __init__(self,horizontal=100,vertical=300,parentwin = None,  flags = 0):
        if parentwin and flags:
            QtGui.QWidget.__init__(self,parentwin,flags)
        else:
            QtGui.QWidget.__init__(self)
        self.valuelist=[]
        self.setObjectName("self")
        self.list=QtGui.QListWidget()
        self.filterbox=QtGui.QLineEdit()
        self.setFixedSize(horizontal+2,vertical+40)
        self.filterbox.setFixedSize(QtCore.QSize(horizontal,25))
        self.list.setFixedSize(QtCore.QSize(horizontal,vertical))
        self.setGeometry(0,0,horizontal,vertical)
        layout=QtGui.QVBoxLayout()
        layout.addWidget(self.list)
        layout.setMargin(0)
        layout.addWidget(self.filterbox)
        self.setLayout(layout)
        self.filterfunction=self.contains
        self.setAttribute(Qt.Qt.WA_TranslucentBackground, True)
        QtCore.QObject.connect(self.filterbox, QtCore.SIGNAL("textChanged(const QString&)"), self.filter)
        QtCore.QObject.connect(self.list, QtCore.SIGNAL("itemClicked(QListWidgetItem*)"), self.ListClicked)
        QtCore.QMetaObject.connectSlotsByName(self)
        self.ConsumableValue=False
        
    def ListClicked(self):
        self.filterbox.setText(self.list.selectedItems()[0].text())
        self.ConsumableValue=True
        self.emit(QtCore.SIGNAL("valueselectiondone()"))
        print "Emitting signal"

    def filter(self):
        self.list.clear()
        for item in self.valuelist:
            if  self.filterfunction(str(item),str(self.filterbox.text())):
                self.list.addItem(item)

    def setvalues(self,listofvalues):
        self.valuelist=listofvalues
        self.filter()

    def getvalues(self):
        return valuelist

    def setfilterfunction(self,filterfunction):
        self.filterfunction=filterfunction

    def startswithfilter(self,te1,te2):
        if te1.startswith(te2):
            return True
        else:
            return False

    def contains(self,te1,te2):
        return te1.__contains__(te2)

    def getvalue(self):
        return self.filterbox.text()

if __name__=="__main__":
    app=QtGui.QApplication(sys.argv)
    fl=FilterableList(400,400)
    fl.setvalues(['ashish','kinnar','hiragc','ashar','at'])
    fl.show()
    fl.setWindowOpacity(0.7)
    sys.exit(app.exec_())

